<?php
namespace app\ClubAdmin\controller;
use think\Db;
use think\Controller;
use think\Session;
class BuyController extends Controller
{
    public function _initialize()
    {
        if (!Session::has('clubid'))
        {
            $this->redirect('index/index/index');
        }
    }

    public function index()
    {

        $c=Db::table('buy')
            ->alias(['buy'=>'a','users'=>'b','lessons'=>'c'])
            ->join('users','b.uid= a.uid')
            ->join('lessons','c.lid= a.lid')
            ->order('bid asc')
            ->paginate(5);
        $this->assign('list', $c);
        return $this->fetch('buy');
    }
    public function show(){
        $bid=input('bid');
        $list=Db::name('buy')->where('bid',$bid)->find();
        echo(json_encode($list)) ;

    }
    public function update()
    {
        $bid = input('bid');
            $data = [
                'bid'=>$bid,
                'bprice' => input('bprice'),
            ];

            if (Db::name('buy')->update($data)) {
                $this->success('更新价格成功！');

            } else
                $this->error('更新价格失败');

        }
    public function edit()
    {
        $bid = input('id');
        $list = Db::name('trainers')->where('bid', $bid)->find();
        echo(json_encode($list));

    }
    public function delete()
    {
        $bid = input('id');
        if (db("buy")->delete($bid)) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }

    }
}